using Catalog.Application.Brands.Interfaces;
using Catalog.Application.Brands.Queries;
using Catalog.Application.Contracts.DTOs;
using MediatR;

namespace Catalog.Application.Brands.QueryHandlers;

public class GetBrandByIdQueryHandler : IRequestHandler<GetBrandByIdQuery, BrandProfileDto?>
{
    private readonly IBrandReadService _brandReadService;

    public GetBrandByIdQueryHandler(IBrandReadService brandReadService)
    {
        _brandReadService = brandReadService;
    }

    public async Task<BrandProfileDto?> Handle(GetBrandByIdQuery request, CancellationToken cancellationToken)
    {
        var brand = await _brandReadService.GetByIdAsync(request.id);
        if (brand == null)
        {
            return null;
        }

        return new BrandProfileDto
        {
            Id = brand.Id,
            Name = brand.Name,
            Description = brand.Description
        };
    }
}